﻿using System.Collections.ObjectModel;
using System.Linq;
using Results.Models;

namespace Results.ViewModels
{
    /// <summary>
    /// The filtered players that should be shown
    /// </summary>
    public class PlayersViewModel : ViewModelBase
    {
        /// <summary>
        /// The filtered players that should be shown
        /// </summary>
        public ObservableCollection<PlayerViewModel> Players { get; private set; }

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="playersModel">The unfiltered source data</param>
        public PlayersViewModel(PlayersModel playersModel)
        {
            Players = new ObservableCollection<PlayerViewModel>();
            _playersModel = playersModel;
        }

        /// <summary>
        /// Populate with all players for the selected group and season
        /// </summary>
        /// <param name="selectedGroup">e.g. "Men 14-16"</param>
        /// <param name="selectedSeason">e.g. "2010"</param>
        /// <param name="notifySelection">Methods to call if the user selects something</param>
        public void Query(string selectedGroup, string selectedSeason, INotifySelection notifySelection)
        {
            Players.Clear();

            foreach (var playerModel in
                from playerModel in _playersModel
                where playerModel.Contains(selectedGroup, selectedSeason)
                select new { PlayerModel = playerModel })
            {
                var playerViewModel = new PlayerViewModel(playerModel.PlayerModel, notifySelection);
                Players.Add(playerViewModel);
            }
        }

        readonly PlayersModel _playersModel;
    }
}
